﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CanDoo.Core;
using CanDoo.Data;
using CanDoo.Extensions;
using CanDoo.Contracts;
using CanDoo.Oil.Core.Models;
using CanDoo.Core.Data;
using CanDoo.Oil.Core.DTOs;
using Mehdime.Entity;

namespace CanDoo.Oil.Core.Contracts.Impl
{
    public class OrderApiService : IOrderApiContract, CanDoo.Core.Dependency.IScopeDependency
    {

        public IDbContextScopeFactory DbScopeFactory { get; set; }

        public IInStoreContract InStoreContract { get; set; }
        public IInBuyBackContract InBuyBackContract { get; set; }
        public IOutStoreContract OutStoreContract { get; set; }
        public IOutSellBackContract OutSellBackContract { get; set; }

        public OrderApiDto GetByOrderNo(string orderNo)
        {
            using (DbScopeFactory.CreateReadOnly())
            {
                var left4Str = orderNo.Left(4).ToUpper();
                if (left4Str == "CGRK")
                    return InStoreContract.SendPDAInfo(orderNo);
                else if (left4Str == "CGTH")
                    return InBuyBackContract.SendPDAInfo(orderNo);
                else if (left4Str == "XSCK")
                    return OutStoreContract.SendPDAInfo(orderNo);
                else if (left4Str == "XSTH")
                    return OutSellBackContract.SendPDAInfo(orderNo);
                else
                {
                    var rr = new OrderApiDto();
                    rr.oType = "单号不存在";
                    rr.oWarehouse = "未知";
                    rr.oSupplier = "未知";
                    return rr;
                }
            }
        }

        public OperationResult Save(OrderApiDto info)
        {
            var left4Str = info.oId.Left(4).ToUpper();
            if (left4Str == "CGRK")
                return InStoreContract.GetPDAData(info);
            else if (left4Str == "CGTH")
                return InBuyBackContract.GetPDAData(info);
            else if (left4Str == "XSCK")
                return OutStoreContract.GetPDAData(info);
            else if (left4Str == "XSTH")
                return OutSellBackContract.GetPDAData(info);
            else
            {
                var result =new OperationResult(OperationResultType.Error,"单据类别错误");
                return result;
            }

        }
    }
}